package com.bugman.managed;

import com.bugman.business.BugService;
import com.bugman.business.ProjectService;
import com.bugman.entity.Bug;
import com.bugman.entity.Project;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.model.SelectItem;

/**
 *
 * @author Ediczech
 */
public class ReportBugBean extends BaseBean {

    private Integer projectId;
    private Bug bug = new Bug();
    private List<SelectItem> projects = new ArrayList<SelectItem>();
    @EJB
    ProjectService projectService;
    @EJB
    BugService bugService;

    @PostConstruct
    public void init() {
        // create List of selectItems
        for (Project project : projectService.getAllProjects()) {
            projects.add(new SelectItem(project.getId(), project.getName()));
        }
    }

    public String report() {
        bug.setProject(projectService.getProject(projectId));
        bug.setFinder(getLoggedUser());
        bugService.createBug(bug);
        logger.info("Bug: \"" + bug + "\" from \"" + bug.getProject() + "\" reported.");
        return "success";
    }

    public Bug getBug() {
        return bug;
    }

    public void setBug(Bug bug) {
        this.bug = bug;
    }

    public Integer getProjectId() {
        return projectId;
    }

    public void setProjectId(Integer projectId) {
        this.projectId = projectId;
    }

    public List<SelectItem> getProjects() {
        return projects;
    }

    public void setProjects(List<SelectItem> projects) {
        this.projects = projects;
    }
}